//
//  SettingsViewController.m
//  Gabber
//
//  Created by 陆惠国 on 11-8-26.
//  Copyright 2011年 恒生聚源. All rights reserved.
//

#import "SettingsViewController.h"
#import "XMPPClient.h"

@implementation SettingsViewController

@synthesize showEmptyGroupSwitch=_showEmptyGroupSwitch;
@synthesize showOfflineUsersSwitch=_showOfflineUsersSwitch;
@synthesize groupOfflineUsersSwitch=_groupOfflineUsersSwitch;
@synthesize openGroupsSwitch=_openGroupsSwitch;

@synthesize playMessageSentSoundSwitch=_playMessageSentSoundSwitch;
@synthesize playMessageReceivedSoundSwitch=_playMessageReceivedSoundSwitch;

@synthesize saveChatHistorySwitch=_saveChatHistorySwitch;
@synthesize showChatAvatarSwitch=_showChatAvatarSwitch;
@synthesize showChatTimestampSwitch=_showChatTimestampSwitch;


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.title = @"设置";
        self.tabBarItem = [[[UITabBarItem alloc] initWithTitle:@"设置" image:[UIImage imageNamed:@"settings.png"] tag:2] autorelease];  
        self.tableViewStyle = UITableViewStyleGrouped;
        self.variableHeightRows = YES;
        
    }
    return self;
}

- (void)dealloc
{
    TT_RELEASE_SAFELY(_showEmptyGroupSwitch);
    TT_RELEASE_SAFELY(_showOfflineUsersSwitch);  
    TT_RELEASE_SAFELY(_groupOfflineUsersSwitch); 
    TT_RELEASE_SAFELY(_openGroupsSwitch); 
    TT_RELEASE_SAFELY(_playMessageSentSoundSwitch);     
    TT_RELEASE_SAFELY(_playMessageReceivedSoundSwitch);       
    
    TT_RELEASE_SAFELY(_saveChatHistorySwitch); 
    TT_RELEASE_SAFELY(_showChatAvatarSwitch);     
    TT_RELEASE_SAFELY(_showChatTimestampSwitch);    
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    DDLogVerbose(@"%@: %@", THIS_FILE, THIS_METHOD); 
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    

    
}

- (void)viewDidUnload
{
    DDLogVerbose(@"%@: %@", THIS_FILE, THIS_METHOD); 
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    TT_RELEASE_SAFELY(_showEmptyGroupSwitch);
    TT_RELEASE_SAFELY(_showOfflineUsersSwitch);  
    TT_RELEASE_SAFELY(_groupOfflineUsersSwitch); 
    TT_RELEASE_SAFELY(_openGroupsSwitch); 
    TT_RELEASE_SAFELY(_playMessageSentSoundSwitch);     
    TT_RELEASE_SAFELY(_playMessageReceivedSoundSwitch);   
    TT_RELEASE_SAFELY(_saveChatHistorySwitch); 
    TT_RELEASE_SAFELY(_showChatAvatarSwitch);     
    TT_RELEASE_SAFELY(_showChatTimestampSwitch); 
}

-(void) viewWillAppear:(BOOL)animated
{
    DDLogVerbose(@"%@: %@", THIS_FILE, THIS_METHOD); 

    [super viewWillAppear:animated];
    self.showEmptyGroupSwitch.on = [[NSUserDefaults standardUserDefaults] boolForKey: kShowEmptyGroup];
    self.showOfflineUsersSwitch.on = [[NSUserDefaults standardUserDefaults] boolForKey: kShowOfflineUsers];
    self.groupOfflineUsersSwitch.on = [[NSUserDefaults standardUserDefaults] boolForKey: kGroupOfflineUsers];
    self.openGroupsSwitch.on = [[NSUserDefaults standardUserDefaults] boolForKey: kOpenGroups];
    
    self.playMessageReceivedSoundSwitch.on = [[NSUserDefaults standardUserDefaults] boolForKey: kPlayMessageReceivedSound];
    self.playMessageSentSoundSwitch.on = [[NSUserDefaults standardUserDefaults] boolForKey: kPlayMessageSentSound];
    
    self.saveChatHistorySwitch.on = [[NSUserDefaults standardUserDefaults] boolForKey: kSaveChatHistory];
    self.showChatAvatarSwitch.on = [[NSUserDefaults standardUserDefaults] boolForKey: kShowChatAvatar];
    self.showChatTimestampSwitch.on = [[NSUserDefaults standardUserDefaults] boolForKey: kShowChatTimestamp];
}

- (void)viewDidDisappear:(BOOL)animated
{
    
    [super viewDidDisappear:animated];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    //return (interfaceOrientation == UIInterfaceOrientationPortrait);
    if (TTIsPad()) {
        return YES;
        
    } else {
        return interfaceOrientation == UIInterfaceOrientationPortrait;
    }
}

#pragma mark - TTModelViewController
- (void)createModel {
    

    if (TTIsPad()) {
        self.dataSource = [TTSectionedDataSource dataSourceWithObjects:
                           @"联系人",
                           [TTTableControlItem itemWithCaption:@"显示空组" control:self.showEmptyGroupSwitch],
                           [TTTableControlItem itemWithCaption:@"显示离线用户" control:self.showOfflineUsersSwitch],
                           [TTTableControlItem itemWithCaption:@"对离线用户分组" control:self.groupOfflineUsersSwitch],
                           [TTTableControlItem itemWithCaption:@"默认打开所有分组" control:self.openGroupsSwitch],
                           @"声音",
                           [TTTableControlItem itemWithCaption:@"收到消息播放声音" control:self.playMessageReceivedSoundSwitch],
                           [TTTableControlItem itemWithCaption:@"发送消息播放声音" control:self.playMessageSentSoundSwitch],
                           @"聊天",
                           [TTTableControlItem itemWithCaption:@"显示头像" control:self.showChatAvatarSwitch],
                           //[TTTableControlItem itemWithCaption:@"显示消息时间" control:self.showChatTimestampSwitch],
                           [TTTableControlItem itemWithCaption:@"保存聊天记录" control:self.saveChatHistorySwitch],
                           
                           [TTTableButton itemWithText:@"清空聊天记录" delegate:self selector:@selector(clearChatHistoryAction:)],
                           nil];
    }else{
        self.dataSource = [TTSectionedDataSource dataSourceWithObjects:
                           @"联系人",
                           [TTTableControlItem itemWithCaption:@"显示空组" control:self.showEmptyGroupSwitch],
                           [TTTableControlItem itemWithCaption:@"显示离线用户" control:self.showOfflineUsersSwitch],
                           [TTTableControlItem itemWithCaption:@"对离线用户分组" control:self.groupOfflineUsersSwitch],
                           [TTTableControlItem itemWithCaption:@"默认打开所有分组" control:self.openGroupsSwitch],
                           @"声音",
                           [TTTableControlItem itemWithCaption:@"收到消息播放声音" control:self.playMessageReceivedSoundSwitch],
                           [TTTableControlItem itemWithCaption:@"发送消息播放声音" control:self.playMessageSentSoundSwitch],
                           @"聊天",
                           [TTTableControlItem itemWithCaption:@"显示头像" control:self.showChatAvatarSwitch],
                           //[TTTableControlItem itemWithCaption:@"显示消息时间" control:self.showChatTimestampSwitch],
                           [TTTableControlItem itemWithCaption:@"保存聊天记录" control:self.saveChatHistorySwitch],
                           
                           [TTTableButton itemWithText:@"清空聊天记录" delegate:self selector:@selector(clearChatHistoryAction:)],
                           
                           @"",
                           [TTTableButton itemWithText:@"重新登录" delegate:self selector:@selector(logoutAction:)],
                           
                           nil];        
    }
    

}

#pragma mark - Fields
- (UISwitch *)showEmptyGroupSwitch
{
	if (_showEmptyGroupSwitch == nil)
	{
        _showEmptyGroupSwitch = [[UISwitch alloc] init];
        [_showEmptyGroupSwitch addTarget:self action:@selector(showEmptyGroupSwitchAction:) forControlEvents:UIControlEventValueChanged];
	}	
	return _showEmptyGroupSwitch;
}

- (UISwitch *)showOfflineUsersSwitch
{
	if (_showOfflineUsersSwitch == nil)
	{
        _showOfflineUsersSwitch = [[UISwitch alloc] init];
        [_showOfflineUsersSwitch addTarget:self action:@selector(showOfflineUsersSwitchAction:) forControlEvents:UIControlEventValueChanged];
	}	
	return _showOfflineUsersSwitch;
}

- (UISwitch *)groupOfflineUsersSwitch
{
	if (_groupOfflineUsersSwitch == nil)
	{
        _groupOfflineUsersSwitch = [[UISwitch alloc] init];
        [_groupOfflineUsersSwitch addTarget:self action:@selector(groupOfflineUsersSwitchAction:) forControlEvents:UIControlEventValueChanged];
	}	
	return _groupOfflineUsersSwitch;
}

- (UISwitch *)openGroupsSwitch
{
	if (_openGroupsSwitch == nil)
	{
        _openGroupsSwitch = [[UISwitch alloc] init];
        [_openGroupsSwitch addTarget:self action:@selector(openGroupsSwitchAction:) forControlEvents:UIControlEventValueChanged];
	}	
	return _openGroupsSwitch;
}

- (UISwitch *)playMessageSentSoundSwitch
{
	if (_playMessageSentSoundSwitch == nil)
	{
        _playMessageSentSoundSwitch = [[UISwitch alloc] init];
        [_playMessageSentSoundSwitch addTarget:self action:@selector(playMessageSentSoundSwitchAction:) forControlEvents:UIControlEventValueChanged];
	}	
	return _playMessageSentSoundSwitch;
}

- (UISwitch *)playMessageReceivedSoundSwitch
{
	if (_playMessageReceivedSoundSwitch == nil)
	{
        _playMessageReceivedSoundSwitch = [[UISwitch alloc] init];
        [_playMessageReceivedSoundSwitch addTarget:self action:@selector(playMessageReceivedSoundSwitchAction:) forControlEvents:UIControlEventValueChanged];
	}	
	return _playMessageReceivedSoundSwitch;
}

- (UISwitch *)saveChatHistorySwitch
{
	if (_saveChatHistorySwitch == nil)
	{
        _saveChatHistorySwitch = [[UISwitch alloc] init];
        [_saveChatHistorySwitch addTarget:self action:@selector(saveChatHistorySwitchAction:) forControlEvents:UIControlEventValueChanged];
	}	
	return _saveChatHistorySwitch;
}

- (UISwitch *)showChatAvatarSwitch
{
	if (_showChatAvatarSwitch == nil)
	{
        _showChatAvatarSwitch = [[UISwitch alloc] init];
        [_showChatAvatarSwitch addTarget:self action:@selector(showChatAvatarSwitchAction:) forControlEvents:UIControlEventValueChanged];
	}	
	return _showChatAvatarSwitch;
}

- (UISwitch *)showChatTimestampSwitch
{
	if (_showChatTimestampSwitch == nil)
	{
        _showChatTimestampSwitch = [[UISwitch alloc] init];
        [_showChatTimestampSwitch addTarget:self action:@selector(showChatTimestampSwitchAction:) forControlEvents:UIControlEventValueChanged];
	}	
	return _showChatTimestampSwitch;
}

#pragma mark - Actions

- (void) showEmptyGroupSwitchAction:(id) sender
{
    [[NSUserDefaults standardUserDefaults] setBool:self.showEmptyGroupSwitch.on forKey: kShowEmptyGroup];
    [[NSNotificationCenter defaultCenter] postNotificationName:kSettingsChangedNotification object:nil];
}

- (void) showOfflineUsersSwitchAction:(id) sender
{
    [[NSUserDefaults standardUserDefaults] setBool:self.showOfflineUsersSwitch.on forKey: kShowOfflineUsers];
    [[NSNotificationCenter defaultCenter] postNotificationName:kSettingsChangedNotification object:nil];
}

- (void) groupOfflineUsersSwitchAction:(id) sender
{
    [[NSUserDefaults standardUserDefaults] setBool:self.groupOfflineUsersSwitch.on forKey: kGroupOfflineUsers];
    [[NSNotificationCenter defaultCenter] postNotificationName:kSettingsChangedNotification object:nil];
}

- (void) openGroupsSwitchAction:(id) sender
{
    [[NSUserDefaults standardUserDefaults] setBool:self.openGroupsSwitch.on forKey: kOpenGroups];
    [[NSNotificationCenter defaultCenter] postNotificationName:kSettingsChangedNotification object:nil];
}

- (void) playMessageSentSoundSwitchAction:(id) sender
{
    [[NSUserDefaults standardUserDefaults] setBool:self.playMessageSentSoundSwitch.on forKey: kPlayMessageSentSound];
    [[NSNotificationCenter defaultCenter] postNotificationName:kSettingsChangedNotification object:nil];
    
}
- (void) playMessageReceivedSoundSwitchAction:(id) sender
{
    [[NSUserDefaults standardUserDefaults] setBool:self.playMessageReceivedSoundSwitch.on forKey:
     kPlayMessageReceivedSound];
    [[NSNotificationCenter defaultCenter] postNotificationName:kSettingsChangedNotification object:nil];
    
}
- (void) saveChatHistorySwitchAction:(id) sender
{
    [[NSUserDefaults standardUserDefaults] setBool:self.saveChatHistorySwitch.on forKey: kSaveChatHistory];
    [[NSNotificationCenter defaultCenter] postNotificationName:kSettingsChangedNotification object:nil];
}

- (void) showChatAvatarSwitchAction:(id) sender
{
    [[NSUserDefaults standardUserDefaults] setBool:self.showChatAvatarSwitch.on forKey: kShowChatAvatar];
    [[NSNotificationCenter defaultCenter] postNotificationName:kSettingsChangedNotification object:nil];
    
}
- (void) showChatTimestampSwitchAction:(id) sender
{
    [[NSUserDefaults standardUserDefaults] setBool:self.showChatTimestampSwitch.on forKey: kShowChatTimestamp];
    [[NSNotificationCenter defaultCenter] postNotificationName:kSettingsChangedNotification object:nil];
    
}

- (void)clearChatHistoryAction:(id) sender
{
    [[XMPPClient sharedInstance] clearAllMessages];
}

- (void)logoutAction:(id) sender
{
//    if (TTIsPad()) {

//    }

    
    [[XMPPClient sharedInstance] disconnect];
    TTOpenURL(@"gabber://login");
}

@end
